/**
 * @Author: 陈瑞鹏
 * @Date:   2022-12-08 15:54:56
 * @Last Modified by:   陈瑞鹏
 * @Last Modified time: 2023-01-11 14:13:27
 */
import gameCtr from "../ctrs/gameCtr";
import netCtr from "../ctrs/netCtr";
import BaseGameLayer from "../libs/core/BaseGameLayer";
import { PureMVCEvents } from "../libs/core/PureMVCEvents";
import { drawScroll2 } from "../libs/core/utils";
import { SceneManager } from "../libs/manager/SceneManager";
import { INotification } from "../libs/pureMvc/Interfaces";

const { ccclass, property } = cc._decorator;
@ccclass
export default class headLayer extends BaseGameLayer {

  @property({ type: cc.Node, tooltip: "头像内容节点" })
  content: cc.Node = null;

  @property({ type: cc.Prefab, tooltip: "头像预制体" })
  headItem: cc.Prefab = null;

  @property({ type: cc.EditBox, tooltip: "昵称修改输入框" })
  nickNameInput: cc.EditBox = null;

  @property({ type: cc.EditBox, tooltip: "兑换码输入框" })
  exchangeInput: cc.EditBox = null;

  onEnable(): void {
    super.onEnable();
    this.drawUI();
  }

  public listNotificationInterests(): string[] {
    return [PureMVCEvents.ON_HEAD_UPDATE];
  }

  public handleNotification(notification: INotification): void {
    let evtName = notification.getName();
    switch (evtName) {
      case PureMVCEvents.ON_HEAD_UPDATE:
        this.drawUI();
        break;
    }
  }

  drawUI() {
    let arr1 = gameCtr.ins.currentBarracks;
    this.content.removeAllChildren();
    drawScroll2(this.content, arr1, this.headItem, "headItemCom", 5, 20, 10);
  }

  onNickUpdate() {
    if (!this.nickNameInput.string) {
      SceneManager.getInstance().pushLayer("tipLayer", "不能为空");
      return;
    }
    netCtr.ins.updateNickName(this.nickNameInput.string).then(() => {
      this.nickNameInput.string = "";
    });
  }

  onExchange() {
    if (!this.exchangeInput.string) {
      SceneManager.getInstance().pushLayer("tipLayer", "不能为空");
      return;
    }
    netCtr.ins.exchange(this.exchangeInput.string).then(() => {
      this.exchangeInput.string = "";
    });
  }
}